package com.dylan.历史.java7;

import java.io.*;
import java.net.UnknownHostException;

/**
 * Created by Dylan on 2018/7/28.
 */
public class 异常处理 {

    public static void main(String[] args) {
        // 使用 try-with-resources语句,当try语句块中的内容执行完后,会自动调用close方法.前提是实现了AutoCloseable接口
        try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("/Users/ankang/test/goods_123.sql")));
            PrintStream ps = new PrintStream(new FileOutputStream("/Users/ankang/test/copy_goods_123.sql"))) {
            String line;
            while ((line = br.readLine()) != null) {
                ps.println(line);
            }
        } catch (FileNotFoundException | UnknownHostException e) { // 多个异常写在一起
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
            // 获取二次二次异常(被忽略的异常)
            Throwable[] secondaryException = e.getSuppressed();
        }

    }

    /**
     * 对于反射方法异常的优化,增加了父类ReflectiveOperationException
     */
    static void testReflectiveOperationException() {
        try {
            Class.forName("异常处理").getMethod("test").invoke(null);
        } catch (ReflectiveOperationException e) {
            e.printStackTrace();
            System.out.println("统一处理这些异常");
        }
//        } catch (IllegalAccessException e) {
//            e.printStackTrace();
//        } catch (InvocationTargetException e) {
//            e.printStackTrace();
//        } catch (NoSuchMethodException e) {
//            e.printStackTrace();
//        } catch (ClassNotFoundException e) {
//            e.printStackTrace();
//        }
    }

    static void test() {
    }
}
